package com.example.core.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Aspect
@Component
//@Slf4j
public class LogAspect {

    Logger log = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut("execution(* com.example.*.controller.*.*(..))")
    public void logPointCut() {}

    @Before("logPointCut()")
    public void logBefore(JoinPoint joinPoint) {
        log.info("【{}】开始执行，参数是: {}", joinPoint.getSignature().getName(), Arrays.toString(joinPoint.getArgs()));
    }

    @AfterReturning(value = "logPointCut()", returning = "result")
    public void logAfterReturning(JoinPoint joinPoint, Object result) {
        log.info("【{}】执行结束，返回值是: {}", joinPoint.getSignature().getName(), result);
    }

    @AfterThrowing(value = "logPointCut()", throwing = "e")
    public void logAfterThrowing(JoinPoint joinPoint, Exception e) {
        log.error("【{}】执行异常，抛出的异常是: 【{}】, 异常信息是: {}", joinPoint.getSignature().getName(),e.getClass().getName(), e.getMessage());
    }

}
